package org.chris.zookeeper;

import com.alibaba.fastjson.JSON;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.data.Stat;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Description: 模拟随机更新节点的数据，同时节点数据watcher监听变化<br/>
 * @version V1.0  by 石冬冬-Heil Hitler on  2017/7/4 15:40
 */
public class ZookeeperTestCase1 {
    public static void main(String[] args) {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    String[] nodes = new String[]{"test1","test2","test3","test4","test5"};
                    String node = nodes[new Random().nextInt(5)];
                    String data = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                    Stat stat = ZookeeperMain.zk.setData("/zookeeper/" + node, data.getBytes(),-1);
                    System.out.println(JSON.toJSONString(stat));
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },0,2000);
    }
}
